package com.md.mainpage.adapter
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.md.livingwidget.flowlabel.PHFlowLabelLayout
import com.md.mainpage.Constants2
import com.md.mainpage.R
import com.md.mainpage.ui.AlbumActivity
import com.md.network.api.Album
import com.md.network.api.Group

class MainCategoryRightAdapter(private var mContext: Context, private var mData:List<Group>):RecyclerView.Adapter<MainCategoryRightAdapter.RightViewHolder>()  {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RightViewHolder {
        var view = LayoutInflater.from(mContext).inflate(R.layout.adapter_right_category,parent,false)
        return RightViewHolder(view)
    }

    override fun getItemCount(): Int {
       return mData.size
    }

    override fun onBindViewHolder(holder: RightViewHolder, position: Int) {
        var curGroup = mData[position]
        var curAlbums = curGroup.albums
        holder.subTitle!!.text = curGroup.name
        holder.flowLayout!!.setLabelTextPadding(15,15,15,15)
        holder.flowLayout!!.showTag(transform(curAlbums)) {
            _, _, pos ->
            var album = curAlbums[pos]
            var albumId = album.id
            var intent = Intent(mContext,AlbumActivity::class.java)
            intent.putExtra(Constants2.ALBUM_ID,albumId)
            mContext.startActivity(intent)
        }
    }

    fun transform(list:List<Album>):List<String>{
        var albumNames:MutableList<String> = ArrayList()
        for (album in list){
            albumNames.add(album.name)
        }
        return albumNames
    }

    fun setData(groups:List<Group>?){
        groups?.let {
            this.mData = groups
        }
        notifyDataSetChanged()
    }


    class RightViewHolder: RecyclerView.ViewHolder{
        var subTitle: TextView?=null
        var flowLayout: PHFlowLabelLayout?=null
        constructor(itemView: View):super(itemView){
            subTitle = itemView.findViewById(R.id.tvSecondTitle)
            flowLayout = itemView.findViewById(R.id.flowLayout)
        }

    }


}